package com.codeanalysissys.backend.cpp;

import com.codeanalysissys.backend.cpp.parser.CppBaseVisitor;
import com.codeanalysissys.backend.cpp.parser.CppLexer;
import com.codeanalysissys.backend.cpp.parser.CppParser;
import ghaffarian.nanologger.Logger;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.tree.ParseTree;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * A utility class for building CppClass and CppMethod structures from a given Cpp source file.
 * 解析出来的class信息和function信息会保留到传进去的参数中
 */
public class CppExtractor {

    public static void extractInfo(String cppFile, List<CppClass> classs, List<CppMethod> functions) throws IOException {
        extractInfo(new File(cppFile), classs, functions);
    }

    public static void extractInfo(File cppFile, List<CppClass> classs, List<CppMethod> functions) throws IOException {
        extractInfo(cppFile.getAbsolutePath(), new FileInputStream(cppFile), classs, functions);
    }

    public static void extractInfo(InputStream inStream, List<CppClass> classs, List<CppMethod> functions) throws IOException {
        ANTLRInputStream input = new ANTLRInputStream(inStream);
        CppLexer lexer = new CppLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        CppParser parser = new CppParser(tokens);
        ParseTree tree = parser.translationUnit();
        extractInfo(tree, classs, functions);
    }

    public static void extractInfo(String cppFilePath, InputStream inStream, List<CppClass> classs, List<CppMethod> functions) throws IOException {
        ANTLRInputStream input = new ANTLRInputStream(inStream);
        CppLexer lexer = new CppLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        CppParser parser = new CppParser(tokens);
        ParseTree tree = parser.translationUnit();
        extractInfo(cppFilePath, tree, classs, functions);
    }

    public static void extractInfo(ParseTree tree, List<CppClass> classs, List<CppMethod> functions) {
        CppVisitor visitor = new CppVisitor();
        visitor.build(tree, classs, functions);
    }

    public static void extractInfo(String cppFilePath, ParseTree tree, List<CppClass> classs, List<CppMethod> functions) {
        CppVisitor visitor = new CppVisitor(cppFilePath);
        visitor.build(tree, classs, functions);
    }

    private static class CppVisitor extends CppBaseVisitor<String> {
        private String filePath;
        private List<CppClass> cppClasses;
        private List<CppMethod> cppFunctions;
        private Deque<CppClass> activeClasses;
        private Deque<String> namespaces;
        private String specifier;
        private String type;
        private String pointOp;
        private String nestedName;
        private String varName;
        private boolean isGlobal;

        public CppVisitor(String path) {
            filePath = path;
        }

        public CppVisitor() {
            filePath = null;
        }

        public void build(ParseTree tree, List<CppClass> classs, List<CppMethod> functions) {
            cppClasses = classs;
            cppFunctions = functions;
            activeClasses = new ArrayDeque<>();
            namespaces = new ArrayDeque<>();
            namespaces.push("");
            specifier = "";
            type = "";
            pointOp = "";
            nestedName = "";
            varName = "";
            clearFlags();
            visit(tree);
        }

        private void clearFlags() {
            isGlobal = false;
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTranslationUnit(CppParser.TranslationUnitContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitPrimaryExpression(CppParser.PrimaryExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitIdExpression(CppParser.IdExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitUnqualifiedId(CppParser.UnqualifiedIdContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitQualifiedId(CppParser.QualifiedIdContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitNestedNameSpecifier(CppParser.NestedNameSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitLambdaExpression(CppParser.LambdaExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitLambdaIntroducer(CppParser.LambdaIntroducerContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitLambdaCapture(CppParser.LambdaCaptureContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitCaptureDefault(CppParser.CaptureDefaultContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitCaptureList(CppParser.CaptureListContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitCapture(CppParser.CaptureContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitSimpleCapture(CppParser.SimpleCaptureContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitInitcapture(CppParser.InitcaptureContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitLambdaDeclarator(CppParser.LambdaDeclaratorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitPostfixExpression(CppParser.PostfixExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTypeIdOfTheTypeId(CppParser.TypeIdOfTheTypeIdContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitExpressionList(CppParser.ExpressionListContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitPseudoDestructorName(CppParser.PseudoDestructorNameContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitUnaryExpression(CppParser.UnaryExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitUnaryOperator(CppParser.UnaryOperatorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitNewExpression(CppParser.NewExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitNewPlacement(CppParser.NewPlacementContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitNewTypeId(CppParser.NewTypeIdContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitNewDeclarator(CppParser.NewDeclaratorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitNoPointerNewDeclarator(CppParser.NoPointerNewDeclaratorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitNewInitializer(CppParser.NewInitializerContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitDeleteExpression(CppParser.DeleteExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitNoExceptExpression(CppParser.NoExceptExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitCastExpression(CppParser.CastExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitPointerMemberExpression(CppParser.PointerMemberExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitMultiplicativeExpression(CppParser.MultiplicativeExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitAdditiveExpression(CppParser.AdditiveExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitShiftExpression(CppParser.ShiftExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitShiftOperator(CppParser.ShiftOperatorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitRelationalExpression(CppParser.RelationalExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitEqualityExpression(CppParser.EqualityExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitAndExpression(CppParser.AndExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitExclusiveOrExpression(CppParser.ExclusiveOrExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitInclusiveOrExpression(CppParser.InclusiveOrExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitLogicalAndExpression(CppParser.LogicalAndExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitLogicalOrExpression(CppParser.LogicalOrExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitConditionalExpression(CppParser.ConditionalExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitAssignmentExpression(CppParser.AssignmentExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitAssignmentOperator(CppParser.AssignmentOperatorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitExpression(CppParser.ExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitConstantExpression(CppParser.ConstantExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitStatement(CppParser.StatementContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitLabeledStatement(CppParser.LabeledStatementContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitExpressionStatement(CppParser.ExpressionStatementContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitCompoundStatement(CppParser.CompoundStatementContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitStatementSeq(CppParser.StatementSeqContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitSelectionStatement(CppParser.SelectionStatementContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitCondition(CppParser.ConditionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitIterationStatement(CppParser.IterationStatementContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitForInitStatement(CppParser.ForInitStatementContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitForRangeDeclaration(CppParser.ForRangeDeclarationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitForRangeInitializer(CppParser.ForRangeInitializerContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitJumpStatement(CppParser.JumpStatementContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitDeclarationStatement(CppParser.DeclarationStatementContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitDeclarationseq(CppParser.DeclarationseqContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitDeclaration(CppParser.DeclarationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitBlockDeclaration(CppParser.BlockDeclarationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitAliasDeclaration(CppParser.AliasDeclarationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitSimpleDeclaration(CppParser.SimpleDeclarationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitStaticAssertDeclaration(CppParser.StaticAssertDeclarationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitEmptyDeclaration(CppParser.EmptyDeclarationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitAttributeDeclaration(CppParser.AttributeDeclarationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitDeclSpecifier(CppParser.DeclSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitDeclSpecifierSeq(CppParser.DeclSpecifierSeqContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitStorageClassSpecifier(CppParser.StorageClassSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitFunctionSpecifier(CppParser.FunctionSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTypedefName(CppParser.TypedefNameContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTypeSpecifier(CppParser.TypeSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTrailingTypeSpecifier(CppParser.TrailingTypeSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTypeSpecifierSeq(CppParser.TypeSpecifierSeqContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTrailingTypeSpecifierSeq(CppParser.TrailingTypeSpecifierSeqContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitSimpleTypeLengthModifier(CppParser.SimpleTypeLengthModifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitSimpleTypeSignednessModifier(CppParser.SimpleTypeSignednessModifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitSimpleTypeSpecifier(CppParser.SimpleTypeSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTheTypeName(CppParser.TheTypeNameContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitDecltypeSpecifier(CppParser.DecltypeSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitElaboratedTypeSpecifier(CppParser.ElaboratedTypeSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitEnumName(CppParser.EnumNameContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitEnumSpecifier(CppParser.EnumSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitEnumHead(CppParser.EnumHeadContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitOpaqueEnumDeclaration(CppParser.OpaqueEnumDeclarationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitEnumkey(CppParser.EnumkeyContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitEnumbase(CppParser.EnumbaseContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitEnumeratorList(CppParser.EnumeratorListContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitEnumeratorDefinition(CppParser.EnumeratorDefinitionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitEnumerator(CppParser.EnumeratorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitNamespaceName(CppParser.NamespaceNameContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitOriginalNamespaceName(CppParser.OriginalNamespaceNameContext ctx) {
            return visitChildren(ctx);
        }

        @Override
        public String visitNamespaceDefinition(CppParser.NamespaceDefinitionContext ctx) {
            if (ctx.Identifier() != null)
                namespaces.push(namespaces.peek() + "::" + ctx.Identifier().getText());
            else if (ctx.originalNamespaceName() != null)
                namespaces.push(namespaces.peek() + "::" + getOriginalCodeText(ctx.originalNamespaceName()));
            else
                namespaces.push(namespaces.peek() + "::" + "");

            return visit(ctx.declarationseq());
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitNamespaceAlias(CppParser.NamespaceAliasContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitNamespaceAliasDefinition(CppParser.NamespaceAliasDefinitionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitQualifiednamespacespecifier(CppParser.QualifiednamespacespecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitUsingDeclaration(CppParser.UsingDeclarationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitUsingDirective(CppParser.UsingDirectiveContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitAsmDefinition(CppParser.AsmDefinitionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitLinkageSpecification(CppParser.LinkageSpecificationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitAttributeSpecifierSeq(CppParser.AttributeSpecifierSeqContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitAttributeSpecifier(CppParser.AttributeSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitAlignmentspecifier(CppParser.AlignmentspecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitAttributeList(CppParser.AttributeListContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitAttribute(CppParser.AttributeContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitAttributeNamespace(CppParser.AttributeNamespaceContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitAttributeArgumentClause(CppParser.AttributeArgumentClauseContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitBalancedTokenSeq(CppParser.BalancedTokenSeqContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitBalancedtoken(CppParser.BalancedtokenContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitInitDeclaratorList(CppParser.InitDeclaratorListContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitInitDeclarator(CppParser.InitDeclaratorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitDeclarator(CppParser.DeclaratorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitPointerDeclarator(CppParser.PointerDeclaratorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitNoPointerDeclarator(CppParser.NoPointerDeclaratorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitParametersAndQualifiers(CppParser.ParametersAndQualifiersContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTrailingReturnType(CppParser.TrailingReturnTypeContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitPointerOperator(CppParser.PointerOperatorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitCvqualifierseq(CppParser.CvqualifierseqContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitCvQualifier(CppParser.CvQualifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitRefqualifier(CppParser.RefqualifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitDeclaratorid(CppParser.DeclaratoridContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTheTypeId(CppParser.TheTypeIdContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitAbstractDeclarator(CppParser.AbstractDeclaratorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitPointerAbstractDeclarator(CppParser.PointerAbstractDeclaratorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitNoPointerAbstractDeclarator(CppParser.NoPointerAbstractDeclaratorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitAbstractPackDeclarator(CppParser.AbstractPackDeclaratorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitNoPointerAbstractPackDeclarator(CppParser.NoPointerAbstractPackDeclaratorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitParameterDeclarationClause(CppParser.ParameterDeclarationClauseContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitParameterDeclarationList(CppParser.ParameterDeclarationListContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitParameterDeclaration(CppParser.ParameterDeclarationContext ctx) {
            return visitChildren(ctx);
        }

        @Override
        public String visitFunctionDefinition(CppParser.FunctionDefinitionContext ctx) {
            //functionDefinition:
            //	attributeSpecifierSeq? declSpecifierSeq? declarator virtualSpecifierSeq? functionBody;
            // 对于嵌套名来说只允许有一个并且是某一个类，这就要求对于非成员函数来说，这个函数的定义式
            // 一定要在对应的命名空间中，而不可以在函数所属命名空间中声明在其他命名空间中定义。暂不支持对
            // 这样子的函数进行处理

            parseDeclSpecifierSeq(ctx.declSpecifierSeq());
            String funcSpec = specifier;
            String funcRet = type;

            parseDeclarator(ctx.declarator());
            funcRet += pointOp;
            String nested = nestedName;
            String funcName = varName;
            if (isGlobal) {
                // 构造函数或析构函数
                nested = type;
                funcRet = "";
            }

            CppParser.ParametersAndQualifiersContext paCtx=null;
            if(ctx.declarator().trailingReturnType()!=null)
            {
                // 函数参数列表后指出函数返回类型的方式
                funcRet=getOriginalCodeText(ctx.declarator().trailingReturnType().trailingTypeSpecifierSeq());
                paCtx=ctx.declarator().parametersAndQualifiers();
            }
            else
            {
                CppParser.NoPointerDeclaratorContext npdCtx = ctx.declarator().pointerDeclarator().noPointerDeclarator();
                paCtx=npdCtx.parametersAndQualifiers();
            }


            //parametersAndQualifiers:
            //	LeftParen parameterDeclarationClause? RightParen cvqualifierseq? refqualifier?
            //		exceptionSpecification? attributeSpecifierSeq?;
            //
            //parameterDeclarationClause:
            //	parameterDeclarationList (Comma? Ellipsis)?;
            List<String> argsList = new ArrayList<>();
            if (paCtx.parameterDeclarationClause() != null) {
                //parameterDeclarationList:
                //	parameterDeclaration (Comma parameterDeclaration)*;
                for (CppParser.ParameterDeclarationContext parmCtx : paCtx
                        .parameterDeclarationClause()
                        .parameterDeclarationList()
                        .parameterDeclaration()) {
                    //parameterDeclaration:
                    //	attributeSpecifierSeq? declSpecifierSeq (
                    //		(declarator | abstractDeclarator?) (
                    //			Assign initializerClause
                    //		)?
                    //	);
                    parseDeclSpecifierSeq(parmCtx.declSpecifierSeq());
                    String argRet = type;
                    if(parmCtx.declarator()==null)
                    {
                        // 函数参数应该（void）
                        break;
                    }
                    parseDeclarator(parmCtx.declarator());
                    argRet += pointOp;
                    argsList.add(argRet);
                }
            }
            int line = ctx.getStart().getLine();
            String[] args = null;
            if (argsList.size() > 0)
                args = argsList.toArray(new String[argsList.size()]);

            if (activeClasses.isEmpty() && nested == "") {
                // 不在class定义式里面的函数定义
                cppFunctions.add(new CppMethod(funcSpec, funcName, namespaces.peek(), funcRet, args, line));
            } else {
                // 在class定义式里面的函数定义
                CppClass cls = null;
                if (nested == "")
                    cls = activeClasses.peek();
                else {
                    for (CppClass c : cppClasses)
                        if (c.NAME.equals(nested)) {
                            cls = c;
                            break;
                        }
                }
                if (cls != null)
                    cls.addMethod(new CppMethod(funcSpec, funcName, cls.NAMESPACE, funcRet, args, line));
            }
            return "";
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitFunctionBody(CppParser.FunctionBodyContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitInitializer(CppParser.InitializerContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitBraceOrEqualInitializer(CppParser.BraceOrEqualInitializerContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitInitializerClause(CppParser.InitializerClauseContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitInitializerList(CppParser.InitializerListContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitBracedInitList(CppParser.BracedInitListContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitClassName(CppParser.ClassNameContext ctx) {
            return visitChildren(ctx);
        }

        @Override
        public String visitClassSpecifier(CppParser.ClassSpecifierContext ctx) {
            //classSpecifier:
            //	classHead LeftBrace memberSpecification? RightBrace;
            //
            //classHead:
            //	classKey attributeSpecifierSeq? (
            //		classHeadName classVirtSpecifier?
            //	)? baseClause?
            //	| Union attributeSpecifierSeq? (
            //		classHeadName classVirtSpecifier?
            //	)?;
            String className = getOriginalCodeText(ctx.classHead().classHeadName());
            CppClass cls = new CppClass(className, namespaces.peek());
            activeClasses.push(cls);
            if (ctx.memberSpecification() != null)
                visit(ctx.memberSpecification());
            cppClasses.add(activeClasses.pop());
            return "";
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitClassHead(CppParser.ClassHeadContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitClassHeadName(CppParser.ClassHeadNameContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitClassVirtSpecifier(CppParser.ClassVirtSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitClassKey(CppParser.ClassKeyContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitMemberSpecification(CppParser.MemberSpecificationContext ctx) {
            return visitChildren(ctx);
        }

        @Override
        public String visitMemberdeclaration(CppParser.MemberdeclarationContext ctx) {
            if (ctx.memberDeclaratorList() != null) {
                // 成员变量声明，也可能是成员函数声明，函数指针变量声明（这个暂不支持）
                parseDeclSpecifierSeq(ctx.declSpecifierSeq());
                String varSpec = specifier;
                String varType = type;

                //memberDeclaratorList:
                //	memberDeclarator (Comma memberDeclarator)*;
                for (CppParser.MemberDeclaratorContext memDecCtx : ctx.memberDeclaratorList().memberDeclarator()) {
                    //memberDeclarator:
                    //	declarator (
                    //		virtualSpecifierSeq? pureSpecifier?
                    //		| braceOrEqualInitializer?
                    //	)
                    //	| Identifier? attributeSpecifierSeq? Colon constantExpression;
                    if (memDecCtx.Identifier() != null) {
                        // 是成员变量声明
                        activeClasses.peek().addField(new CppField(varSpec, varType, memDecCtx.Identifier().getText()));
                    } else if (memDecCtx.declarator() != null) {
                        // 可能函数声明或者变量声明
                        if (memDecCtx.declarator().pointerDeclarator().noPointerDeclarator().parametersAndQualifiers() != null) {
                            // 函数声明
                            break;
                        } else {
                            // 变量声明
                            parseDeclarator(memDecCtx.declarator());
                            activeClasses.peek().addField(new CppField(varSpec, varType + pointOp, varName));
                        }
                    } else
                        break;
                }

            } else
                return visitChildren(ctx);

            return "";
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitMemberDeclaratorList(CppParser.MemberDeclaratorListContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitMemberDeclarator(CppParser.MemberDeclaratorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitVirtualSpecifierSeq(CppParser.VirtualSpecifierSeqContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitVirtualSpecifier(CppParser.VirtualSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitPureSpecifier(CppParser.PureSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitBaseClause(CppParser.BaseClauseContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitBaseSpecifierList(CppParser.BaseSpecifierListContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitBaseSpecifier(CppParser.BaseSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitClassOrDeclType(CppParser.ClassOrDeclTypeContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitBaseTypeSpecifier(CppParser.BaseTypeSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitAccessSpecifier(CppParser.AccessSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitConversionFunctionId(CppParser.ConversionFunctionIdContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitConversionTypeId(CppParser.ConversionTypeIdContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitConversionDeclarator(CppParser.ConversionDeclaratorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitConstructorInitializer(CppParser.ConstructorInitializerContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitMemInitializerList(CppParser.MemInitializerListContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitMemInitializer(CppParser.MemInitializerContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitMeminitializerid(CppParser.MeminitializeridContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitOperatorFunctionId(CppParser.OperatorFunctionIdContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitLiteralOperatorId(CppParser.LiteralOperatorIdContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTemplateDeclaration(CppParser.TemplateDeclarationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTemplateparameterList(CppParser.TemplateparameterListContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTemplateParameter(CppParser.TemplateParameterContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTypeParameter(CppParser.TypeParameterContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitSimpleTemplateId(CppParser.SimpleTemplateIdContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTemplateId(CppParser.TemplateIdContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTemplateName(CppParser.TemplateNameContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTemplateArgumentList(CppParser.TemplateArgumentListContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTemplateArgument(CppParser.TemplateArgumentContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTypeNameSpecifier(CppParser.TypeNameSpecifierContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitExplicitInstantiation(CppParser.ExplicitInstantiationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitExplicitSpecialization(CppParser.ExplicitSpecializationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTryBlock(CppParser.TryBlockContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitFunctionTryBlock(CppParser.FunctionTryBlockContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitHandlerSeq(CppParser.HandlerSeqContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitHandler(CppParser.HandlerContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitExceptionDeclaration(CppParser.ExceptionDeclarationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitThrowExpression(CppParser.ThrowExpressionContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitExceptionSpecification(CppParser.ExceptionSpecificationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitDynamicExceptionSpecification(CppParser.DynamicExceptionSpecificationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTypeIdList(CppParser.TypeIdListContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitNoeExceptSpecification(CppParser.NoeExceptSpecificationContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitTheOperator(CppParser.TheOperatorContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * {@inheritDoc}
         *
         * <p>The default implementation returns the result of calling
         * {@link #visitChildren} on {@code ctx}.</p>
         */
        @Override
        public String visitLiteral(CppParser.LiteralContext ctx) {
            return visitChildren(ctx);
        }

        /**
         * 用于解析declSpecifierSeq从中获取到specifier和typename
         */
        private void parseDeclSpecifierSeq(CppParser.DeclSpecifierSeqContext ctx) {
            //declSpecifierSeq: declSpecifier+? attributeSpecifierSeq?;
            specifier = "";
            type = "";
            if (ctx != null && ctx.declSpecifier() != null) {
                for (CppParser.DeclSpecifierContext decCtx : ctx.declSpecifier()) {
                    //declSpecifier:
                    //	storageClassSpecifier
                    //	| typeSpecifier
                    //	| functionSpecifier
                    //	| Friend
                    //	| Typedef
                    //	| Constexpr;
                    if (decCtx.typeSpecifier() != null)
                        type += getOriginalCodeText(decCtx) + " ";
                    else
                        specifier += getOriginalCodeText(decCtx) + " ";
                }
                if (!type.equals(""))
                    type = type.substring(0, type.length() - 1);
            }
        }

        /**
         * 用于解析declarator从中获取到pointOp、nestedName和varName
         */
        private void parseDeclarator(CppParser.DeclaratorContext ctx) {

            //declarator:
            //	pointerDeclarator
            //	| noPointerDeclarator parametersAndQualifiers trailingReturnType;
            //
            //pointerDeclarator: (pointerOperator Const?)* noPointerDeclarator;
            clearFlags();
            pointOp = "";
            CppParser.DeclaratoridContext decIdCtx=null;
            if(ctx.trailingReturnType()!=null)
            {
                decIdCtx=ctx.noPointerDeclarator().declaratorid();
            }
            else
            {
                CppParser.PointerDeclaratorContext pdCtx = ctx.pointerDeclarator();
                if (pdCtx.pointerOperator() != null) {
                    for (int i = 0; i < pdCtx.pointerOperator().size(); ++i) {
                        pointOp += getOriginalCodeText(pdCtx.pointerOperator(i));
                        if (pdCtx.Const(i) != null)
                            pointOp += pdCtx.Const(i).getText();
                    }
                }

                //noPointerDeclarator:
                //	declaratorid attributeSpecifierSeq?
                //	| noPointerDeclarator (
                //		parametersAndQualifiers
                //		| LeftBracket constantExpression? RightBracket attributeSpecifierSeq?
                //	)
                //	| LeftParen pointerDeclarator RightParen;
                if (pdCtx.noPointerDeclarator().LeftParen() != null) {
                    //	| LeftParen pointerDeclarator RightParen;
                    nestedName = "";
                    varName = "";
                    return;
                }

                if (pdCtx.noPointerDeclarator().parametersAndQualifiers() != null || pdCtx.noPointerDeclarator().LeftParen() != null) {
                    if(pdCtx.noPointerDeclarator().parametersAndQualifiers() != null && pdCtx.noPointerDeclarator().noPointerDeclarator().LeftParen()!=null)
                    {
                        // 可能函数签名
                        CppParser.PointerDeclaratorContext tmpPdCtx=pdCtx.noPointerDeclarator().noPointerDeclarator().pointerDeclarator();
                        decIdCtx=tmpPdCtx.noPointerDeclarator().declaratorid();
                        if (tmpPdCtx.pointerOperator() != null) {
                            for (int i = 0; i < tmpPdCtx.pointerOperator().size(); ++i) {
                                pointOp += getOriginalCodeText(tmpPdCtx.pointerOperator(i));
                                if (tmpPdCtx.Const(i) != null)
                                    pointOp += tmpPdCtx.Const(i).getText();
                            }
                        }
                        pointOp+=getOriginalCodeText(pdCtx.noPointerDeclarator().parametersAndQualifiers());

                    }
                    else
                    {
                        CppParser.NoPointerDeclaratorContext npdCtx = pdCtx.noPointerDeclarator().noPointerDeclarator();
                        while (npdCtx.LeftParen() != null) {
                            //noPointerDeclarator:
                            //	| LeftParen pointerDeclarator RightParen;
                            npdCtx = npdCtx.pointerDeclarator().noPointerDeclarator();
                        }
                        decIdCtx = npdCtx.declaratorid();
                    }
                }
                else if(pdCtx.noPointerDeclarator().LeftBracket()!=null)
                {
                    CppParser.NoPointerDeclaratorContext npdCtx = pdCtx.noPointerDeclarator().noPointerDeclarator();
                    while (npdCtx.LeftBracket() != null) {
                        //noPointerDeclarator:
                        //  noPointerDeclarator
                        //		 LeftBracket constantExpression? RightBracket attributeSpecifierSeq?
                        npdCtx = npdCtx.noPointerDeclarator();
                    }
                    decIdCtx = npdCtx.declaratorid();
                }else
                    decIdCtx = pdCtx.noPointerDeclarator().declaratorid();
            }



            if(decIdCtx==null)
            {
                Logger.error("decIdCtx==null");
                Logger.error(filePath+" line : "+ctx.getStart().getLine());
                Logger.error("code : "+getOriginalCodeText(ctx));
            }

            //declaratorid: Ellipsis? idExpression;
            //
            //idExpression: unqualifiedId | qualifiedId;
            nestedName = "";
            varName = "";
            if (decIdCtx.idExpression().qualifiedId() != null) {
                // 存在嵌套名，当前只允许有一个
                varName = getOriginalCodeText(decIdCtx.idExpression().qualifiedId().unqualifiedId());
                nestedName = getOriginalCodeText(decIdCtx.idExpression().qualifiedId().nestedNameSpecifier().theTypeName());
                if (nestedName == "")
                    isGlobal = true;
            } else
                varName = getOriginalCodeText(decIdCtx.idExpression().unqualifiedId());
        }

        private String getOriginalCodeText(ParserRuleContext ctx) {
            if (ctx == null)
                return "";
            int start = ctx.start.getStartIndex();
            int stop = ctx.stop.getStopIndex();
            Interval interval = new Interval(start, stop);
            return ctx.start.getInputStream().getText(interval);
        }

    }

}
